<?php

namespace app\models\Child;

use Yii;

/**
 * This is the model class for table "t_reply_info".
 *
 * @property integer $reply_id
 * @property string $order_no
 * @property integer $doctor_id
 * @property integer $user_id
 * @property string $user_name
 * @property string $user_img
 * @property integer $reply_integral
 * @property string $reply_info
 * @property string $create_time
 *
 * @property TUserInfo $user
 * @property SysUserInfo $doctor
 */
class TReplyInfo extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 't_reply_info';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['order_id','doctor_id', 'user_id', 'reply_integral','gentle_integral','service_integral','subclinic_integral'], 'integer'],
            [['create_time'], 'safe'],
            [['user_name'], 'string', 'max' => 50],
            [['user_img'], 'string', 'max' => 100],
            [['reply_info'], 'string', 'max' => 500],
            [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => TUserInfo::className(), 'targetAttribute' => ['user_id' => 'user_id']],
            [['doctor_id'], 'exist', 'skipOnError' => true, 'targetClass' => SysUserInfo::className(), 'targetAttribute' => ['doctor_id' => 'user_id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'reply_id' => 'Reply ID',
            'order_id' => 'Order ID',
            'doctor_id' => 'Doctor ID',
            'user_id' => 'User ID',
            'user_name' => 'User Name',
            'user_img' => 'User Img',
            'reply_integral' => 'Reply Integral',//有效性
            'gentle_integral' => 'Gentle Integral',//亲和力
            'service_integral' => 'Service Integral',//服务态度
            'subclinic_integral' => 'Subclinic Integral',//店内环境
            'reply_info' => 'Reply Info',
            'create_time' => 'Create Time',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(TUserInfo::className(), ['user_id' => 'user_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getDoctor()
    {
        return $this->hasOne(SysUserInfo::className(), ['user_id' => 'doctor_id']);
    }
    
    /*
     * 对医生的服务进行评价
     */
    public function setReply($order_id,$doctor_id,$user_id,$user_name,$reply_integral,
                             $gentle_integral,$service_integral,$subclinic_integral,$reply_info,$time)
    {
        $this->order_id = $order_id;
        $this->doctor_id = $doctor_id;
        $this->user_id = $user_id;
        $this->user_name = $user_name;
        $this->reply_integral = $reply_integral;
        $this->gentle_integral = $gentle_integral;
        $this->service_integral = $service_integral;
        $this->subclinic_integral = $subclinic_integral;
        $this->reply_info = $reply_info;
        $this->create_time = $time;
        
        return $this->save();
    }
    
    /*
     * 获取所有医生的评价信息
     */
    public function getIntegral()
    {
        $integrals = $this->find()
                ->select('doctor_id,count(reply_id) as integral_count,sum(reply_integral+gentle_integral+service_integral+subclinic_integral) as integral_sum')
                ->groupBy('doctor_id')
                ->asArray()
                ->all();
        return $integrals;
    }
    /*
     * 根据分店获取医生的评价信息
     */
    public function getIntegralGroupBySubclinicId($subclinic_id)
    {
        $integrals = $this->find()
            ->select('doctor_id,count(reply_id) as integral_count,sum(reply_integral+gentle_integral+service_integral+subclinic_integral) as integral_sum')
            ->where(['subclinic_id' => $subclinic_id])
            ->asArray()
            ->all();
        return $integrals;
    }
    /*
     * 评价
     */
    public function getList($booking_id)
    {
        $list = $this->find()
                ->where(['booking_id' => $booking_id])
                ->asArray()
                ->one();
        return $list;
    }
    
}
